/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Inter;

import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Scanner;

/**
 *
 * @author Josue
 */
public class Interprete {

    static LinkedList<Defun> funciones = new LinkedList();
    static Aritmetica arit = new Aritmetica();
    
    
    public Interprete(){
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        /*
         * SECCION DE INSTRUCCIONES ------------------------------------
         */
        
        
        do{
            String[] ingreso = obtenerString("-->",true);
            if (ingreso == null) break;
            if(ingreso[0].equals("(+") ){
               Double n1 = new Double(ingreso[1]);
               Double n2 = new Double(ingreso[2].charAt(0));
               n2 = n2-48;
               System.out.println(arit.sumar(n1, n2));
            }
            if(ingreso[0].equals("(-")){
               Double n1 = new Double(ingreso[1]);
               Double n2 = new Double(ingreso[2].charAt(0));
               n2 = n2-48;
                System.out.println(""+arit.restar(n1, n2));
            }
            if(ingreso[0].equals("(*")){
               Double n1 = new Double(ingreso[1]);
               Double n2 = new Double(ingreso[2].charAt(0));
               n2 = n2-48;
                System.out.println(arit.multi(n1, n2));
            }
            if(ingreso[0].equals("(/")){
               Double n1 = new Double(ingreso[1]);
               Double n2 = new Double(ingreso[2].charAt(0));
               n2 = n2-48;
                System.out.println(arit.dividir(n1, n2));
            }
            if((ingreso[0].equals("(Defun"))||(ingreso[0].equals("(DEFUN"))||(ingreso[0].equals("(defun"))){
                //String[] ingreso2 = ingreso[1].split(" ");
                //System.out.print(ingreso[1]);
                String uno = ingreso[1];
                String dos = ingreso[2].substring(1, (ingreso[2].length()-1));
                //System.out.println(dos);
                String[] tres = new String[20];
                int u = 0;
                for (int y=3;y<ingreso.length;y++){
                   // System.out.print("ingreso: "+ingreso[y]);
                    tres[u] = ingreso[y];
                    //System.out.print("    ");
                    //System.out.println("Tres: "+tres[u]);
                    u++;
                }
                    
                Defun func = new Defun(uno,dos,tres);
                funciones.add(func);
            }
            
            for(int o =0;o<funciones.size();o++){
                //System.out.println(funciones.get(o).nombre);
                //System.out.println(ingreso[0]);
                if ((ingreso[0].substring(1)).equals(funciones.get(o).nombre)){
                    String ingreso3 = ingreso[1].replace(')', '.');
                    double val = new Double(ingreso3);
                    //System.out.println(val);
                    System.out.println(funciones.get(o).operar(val));
                }
            }
            
        }while(true);
    }
    
    public static String[] obtenerString(String mensaje, boolean mostrar){
        Scanner ingreso = new Scanner(System.in);
        String strng = null;
        
        System.out.print(mensaje);
        try{
            strng = ingreso.nextLine();
        }
        catch(InputMismatchException exception){
            if (mostrar)
                System.out.println("\nERROR: ~~> Dato invalido. :O(\n");
        }
        finally{
            if((strng.equals("end"))||(strng.equals("END"))||(strng.equals("End"))){
            return null;
            }
        }
        return strng.split(" ");
    }
}
